package wujinye.com.dao;

import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.RowProcessor;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import wujinye.com.constant.Const;
import wujinye.com.dao.base.BaseDao;
import wujinye.com.model.UserFocus;
import wujinye.com.utils.JDBCUtil;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class UserFocusDao extends BaseDao<UserFocus> {

    /**
     * 查看数据库是否已存在该组数据
     *
     * @param uId
     * @param ufId
     * @return
     */
    public UserFocus findFocusBean(Integer uId, Integer ufId) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        RowProcessor processor = new BasicRowProcessor(new GenerousBeanProcessor());
        UserFocus userFocus = null;
        try {
            String sql = "select * from user_focus where user_id = ? and user_focus_id = ?;";
            userFocus = qr.query(sql, new BeanHandler<>(UserFocus.class, processor), uId, ufId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userFocus;
    }

    /**
     * 添加关注
     *
     * @param userId
     * @param ufId
     */
    public void addFocus(Integer userId, Integer ufId) {
        String sql = "insert into user_focus values(?,?,?);";
        Object[] obj = {null, userId, ufId};
        adu(sql, obj);
    }

    /**
     * 删除关注
     *
     * @param userId
     * @param ufId
     */
    public void removeFocus(Integer userId, Integer ufId) {
        String sql = "delete from user_focus where user_id = ? and user_focus_id = ?;";
        Object[] obj = {userId, ufId};
        adu(sql, obj);
    }

    /**
     * 查找用户所关注的用户
     *
     * @param userId
     * @param page
     * @return
     */
    public List<Map<String, Object>> findFocusList(Integer userId, Integer page) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        List<Map<String, Object>> list = null;
        try {
            String sql = "select uf.user_id as uId,uf.user_focus_id as ufId,ui.real_name as realName,u.username\n" +
                    "from user_focus uf left join user_info ui on uf.user_focus_id = ui.user_id\n" +
                    "left join user u on uf.user_focus_id = u.id\n" +
                    "where uf.user_id = ?\n" +
                    "order by uf.user_focus_id limit ?,?";
            list = qr.query(sql, new MapListHandler(), userId, (page - 1) * Const.MAX_USER_PAGE_SIZE, Const.PAGE_COUNT);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public long getFocusTotalCount(Integer userId) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        long total = 0L;
        try{
            String sql = "select count(*) from user_focus where user_id = ?;";
            total = qr.query(sql,new ScalarHandler<Long>(),userId);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return total;
    }
}
